/*	This program produces Appendix Table D.3 */

***** Set directories 
local dir_clean 	"~/Dropbox/Retirement gaming/clean"
local dir_output 	"~/Dropbox/Retirement gaming/output/dataverse"

use "`dir_clean'/mainsample_medbcw.dta", clear

* Sets of controls		
local listcontrolsDD1  = " i.t i.t#i.self_empl " 
local listcontrolsDD2  = " i.t i.t#i.self_empl i.ndep_cat_1stobs##i.self_empl i.ciiu2_1stobs##i.self_empl i.ndep_cat##i.self_empl " 
local listcontrolsDD3  = " i.t	i.t#i.self_empl i.ndep_cat_1stobs##i.self_empl i.ciiu2_1stobs##i.self_empl i.ndep_cat##i.self_empl 	i.year#i.ciiu2_1stobs i.year#i.ndep_cat_1stobs i.year#i.ciiu2_1stobs#i.self_empl i.year#i.ndep_cat_1stobs#i.self_empl" 
local listcontrolsDDE1 = " i.t i.t#i.small_1stobs " 
local listcontrolsDDE2 = " i.t	i.t#i.small_1stobs i.ndep_cat_1stobs##i.small_1stobs i.ciiu2_1stobs##i.small_1stobs i.ndep_cat##i.small_1stobs " 
local listcontrolsDDE3 = " i.t	i.t#i.small_1stobs i.ndep_cat_1stobs##i.small_1stobs i.ciiu2_1stobs##i.small_1stobs i.ndep_cat##i.small_1stobs i.year#i.ciiu2_1stobs i.year#i.ndep_cat_1stobs i.year#i.ciiu2_1stobs#i.small_1stobs i.year#i.ndep_cat_1stobs#i.small_1stobs " 

estimates clear
foreach c in  DD1 DD2 DD3 {
	preserve
	keep if (empl==1 &  small_1stobs==0)| (self_empl==1)
	*Normalize wages for each group
	sum W if time_bcw==-1  &  empl==1
	local meanw=r(mean)
	g Wz=W/`meanw' 
	sum W if time_bcw==-1  &  self_empl==1
	local meanw=r(mean) 
	replace Wz=W/`meanw' if self_empl==1
	bysort self_empl: sum Wz if time_bcw==-1
	*Rename vars for table
	foreach X in  pre6 pre25 post03 post4 {
	rename `X'_self_empl `X'_did
	}	
	* Regress
	reghdfe Wz pre6 pre25 post03 post4 pre6_did pre25_did post03_did post4_did , absorb(`listcontrolsDD3') 
	g thissample=e(sample)
	eststo: reghdfe Wz  pre6 pre25 post03 post4 pre6_did pre25_did post03_did post4_did   if thissample==1, absorb( `listcontrols`c'') vce(cluster i) 
	restore
}
foreach c in DDE1 DDE2 DDE3 {
	preserve
	keep if empl==1 
	*Normalize wages
	sum W if time_bcw==-1 & small_1stobs==0
	local meanw=r(mean)
	g Wz=W/`meanw'
	sum W if time_bcw==-1 & small_1stobs==1
	local meanw=r(mean)
	replace Wz=W/`meanw' if small_1stobs==1
	*Rename vars for table
	foreach X in pre6 pre25 post03 post4 {
	rename `X'_small `X'_did
	}	
	* Regress
	reghdfe Wz  pre6 pre25 post03 post4 pre6_did pre25_did post03_did post4_did , absorb(`listcontrolsDDE3') 
	g thissample=e(sample)
	eststo: reghdfe Wz  pre6 pre25 post03 post4 pre6_did pre25_did post03_did post4_did  if thissample==1, absorb(`listcontrols`c'') vce(cluster i )
	restore
}	
* Table with results 
esttab using "`dir_output'/tableD3.tex", ///
	keep(pre25_did post03_did post4_did)   ///
	stats(N N_clust1, label("Observations" "Individuals") fmt(%9.0gc)) prefoot(\hline) ///  
	b(%4.3f) se(%4.3f) star(* 0.10 ** 0.05 *** 0.01) ///
	coeflabels(pre25_did "2-5 yrs. pre BCW" post03_did "0-3 yrs. post BCW" post4_did "4+ yrs. post BCW") ///	
	f nomtitles  nogaps replace label nonumbers nolines		


 
clear all
exit
